package com.ml.refactor;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {
	
	private String _name;
	private Vector _rentals = new Vector();
	
	public Customer(String name) {
		_name = name;
	}
	
	public void addRental(Rental rental) {
		_rentals.addElement(rental);
	}
	
	public String getName() {
		return _name;
	}
	
	
	public String  Statements() {
		String result ="Rental record for "+getName()+"\n";
		
		Enumeration rentals = _rentals.elements();
		
		while(rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			
			//这一步骤是做积分计算
			
			result +="\t" + each.getMovie().getTitle() + "\t"+String.valueOf(each.getCharge()) + "\n";
		}
		
		result +="Amount owed is "+ String.valueOf(getTotalAmount()) +"\n";
		result += "You earned " + String.valueOf(getTotalFrequenRenterPoints())+
				" frequent renter points";
		
		return result;
	}
	
	/**
	 * 计算费用总和
	 * @return
	 */
	private double getTotalAmount() {
		double result = 0;
		Enumeration rentals = _rentals.elements();
		
		while(rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result +=each.getCharge();
		}
		
		return result;
	}
	
	private int getTotalFrequenRenterPoints() {
		int result = 0;
		
		Enumeration rentals = _rentals.elements();
		
		while(rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result += each.getFrequenRenterPoints();
		}
		
		return result;
	}
	
}
